package com.lvjunxiong.serviceinterface;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lvjunxiong.serviceinterface.bi.BIModel;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import utils.MD5Utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvjunxiong on 2019/5/16.
 */
public class BIUtil {
    private static Logger logger = Logger.getLogger(BIUtil.class);

    public static String doPost(String url, String params) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);// 创建httpPost
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-Type", "application/json");
        String charSet = "UTF-8";
        StringEntity entity = new StringEntity(params, charSet);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpPost);
            StatusLine status = response.getStatusLine();
            int state = status.getStatusCode();
            System.out.println(state + "=====");
            if (state == HttpStatus.SC_OK) {
                HttpEntity responseEntity = response.getEntity();
                String jsonString = EntityUtils.toString(responseEntity);
                return jsonString;
            } else {
                logger.error("请求返回:" + state + "(" + url + ")");
            }
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String md5(String text, String key) throws Exception {
        //加密后的字符串
        String encodeStr = DigestUtils.md5Hex(text + key);
        System.out.println("MD5加密后的字符串为:encodeStr=" + encodeStr);
        return encodeStr;
    }


    public static void main(String[] args) throws Exception {
        long time =System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String res = sdf.format(time);
        String appmid = "sxpg001";
        String md5info = "wxmp";
        String mobnum = "13810381138";
//        String mobnum = "18310853824";
        String timestamp = res;
        String source = appmid + md5info + mobnum + timestamp;
        System.out.println("打印出来的合并的串："+source);
        String echostr = MD5Utils.MD5Encode(source.toLowerCase(),"utf8");
        String st = "https://weixin.shuhai.cc/io/usercheckit.cfm?mobnum=" + mobnum + "&timestamp=" + timestamp + "&echostr=" + echostr + "";
        System.out.println(st + "==");
        String ff = doPost(st,"");
        System.out.println("用户信息接口返回结果："+ff);
        JSONObject jo_0 = JSONObject.fromObject(ff);
        System.out.println(jo_0.toString());
        List<BIModel> list = new ArrayList<BIModel>();
        if(jo_0.getString("validate").equals("true")){
            JSONArray rows = jo_0.getJSONArray("data");
            if(rows.size()>0){
                System.out.println("有");
                for (int i = 0; i < rows.size(); i++) {
                    String res_ = rows.getString(i);
                    JsonParser jps = new JsonParser();
                    JsonObject jos = jps.parse(res_).getAsJsonObject();
                    String xh = jos.get("xh").getAsString();//获取的xh
                    String bmid = jos.get("bmid").getAsString();//获取的bmid
                    String userid = jos.get("userid").getAsString();//获取的userid
                    String username = jos.get("username").getAsString();//获取的username
                    String jtid = jos.get("jtid").getAsString();//获取的jtid
                    String jtname = jos.get("jtname").getAsString();//获取的jtname
                    System.out.println("xh:" + xh + ",bmid:" + bmid + ",userid:" + userid + ",username:" + username + ",jtid:" + jtid + ",jtname:" + jtname);
                    BIModel model = new BIModel();
                    model.setXh(xh);
                    model.setBmid(bmid);
                    model.setUserid(userid);
                    model.setUsername(username);
                    model.setJtid(jtid);
                    model.setJtname(jtname);
                    list.add(model);
                }
            }else{
                System.out.println("无");
            }
            for (int i = 0; i < list.size(); i++) {
                BIModel mm = list.get(i);
                System.out.println("拿到的数据："+mm.getXh());
            }
        }else{
            System.out.println("暂时还未为您开通该权限");
        }

        String bmid = "00";
        String userid = "test1";
        String jtid = "00";
        String source_ = appmid + md5info + mobnum + bmid + userid + jtid + timestamp;
        String echostr_ = MD5Utils.MD5Encode(source_.toLowerCase(), "utf8");


        String str = "https://weixin.shuhai.cc/io/wxpmlogin.cfm?mobnum=" + mobnum + "&xh=1&timestamp=" + timestamp + "&echostr=" + echostr_ + "";
        System.out.println(str);

        String auto = doPost(str, "");
        System.out.println(auto);


    }
}
